package com.ywb.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.ywb.order.service.PaymentFallBackService;
import com.ywb.order.service.PaymentHystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "paymentTimeoutHandler")
public class OrderController {
    @Autowired
    private PaymentHystrixService paymentHystrixService;
    @GetMapping("/payment/hystrix/ok/{id}")
    public String ok(@PathVariable("id") Integer id){
        return paymentHystrixService.ok(id);
    }

    @HystrixCommand(fallbackMethod = "paymentTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "150")
    })
    @GetMapping("/paymnet/hystrix/timeout/{id}")
    public String timeout(@PathVariable("id") Integer id){
        return paymentHystrixService.timeout(id);
    }

    public String paymentTimeoutHandler(@PathVariable("id") Integer id){
        return "我是消费者端口的服务熔断";
    }
}
